package com.fengye.algorithms.leecode.dp;

/**
 * @Author fengye
 * @Date 2023/5/3 5:54
 **/
public class ArithmeticSlices413 {
    public static void main(String[] args) {
        System.out.println(new ArithmeticSlices413().numberOfArithmeticSlices(new int[]{1,2,3,4,6,7,8}));
    }

    public int numberOfArithmeticSlices(int[] nums) {
        if(nums.length <= 2) {
            return 0;
        }

        int count = 0, t = 0;
        int d = nums[1] - nums[0];
        for(int i=2;i<nums.length;i++) {
            if(nums[i] - nums[i-1] == d) {
                t++;
            } else {
                d = nums[i] - nums[i-1];
                t = 0;
            }
            // t每增加一个，对应的count也增加t个
            count += t;
        }
        return count;
    }

    public int numberOfArithmeticSlices2(int[] nums) {
        int n=nums.length;
        if(n<3)return 0;
        // 第i个元素结尾的子数组有多少个
        int[]f=new int[n];
        int ans=0;
        for(int i=2;i<n;i++){
            if(nums[i]-nums[i-1]==nums[i-1]-nums[i-2]){
                f[i]=f[i-1]+1;
                ans+=f[i];
            }
        }
        return ans;
    }
}
